Option to set response code for webhook agent (#1676)

* setting for webhook response code

* some adjustments

Thiago Talma 8 lat temu
rodzic
commit
c3d5380f98
2 zmienionych plików z 29 dodań i 1 usunięć
  1. 9 1
      app/models/agents/webhook_agent.rb
  2. 20 0
      spec/models/agents/webhook_agent_spec.rb

+ 9 - 1
app/models/agents/webhook_agent.rb

@@ -26,6 +26,7 @@ module Agents
26 26
           For example, "post,get" will enable POST and GET requests. Defaults
27 27
           to "post".
28 28
         * `response` - The response message to the request. Defaults to 'Event Created'.
29
+        * `code` - The response code to the request. Defaults to '201'.
29 30
         * `recaptcha_secret` - Setting this to a reCAPTCHA "secret" key makes your agent verify incoming requests with reCAPTCHA.  Don't forget to embed a reCAPTCHA snippet including your "site" key in the originating form(s).
30 31
         * `recaptcha_send_remote_addr` - Set this to true if your server is properly configured to set REMOTE_ADDR to the IP address of each visitor (instead of that of a proxy server).
31 32
       MD
@@ -53,6 +54,9 @@ module Agents
53 54
       # check the verbs
54 55
       verbs = (interpolated['verbs'] || 'post').split(/,/).map { |x| x.strip.downcase }.select { |x| x.present? }
55 56
       return ["Please use #{verbs.join('/').upcase} requests only", 401] unless verbs.include?(method)
57
+      
58
+      # check the code
59
+      code = (interpolated['code'].presence || 201).to_i
56 60
 
57 61
       # check the reCAPTCHA response if required
58 62
       if recaptcha_secret = interpolated['recaptcha_secret'].presence
@@ -84,7 +88,7 @@ module Agents
84 88
         create_event(payload: payload)
85 89
       end
86 90
 
87
-      [response_message, 201]
91
+      [response_message, code]
88 92
     end
89 93
 
90 94
     def working?
@@ -95,6 +99,10 @@ module Agents
95 99
       unless options['secret'].present?
96 100
         errors.add(:base, "Must specify a secret for 'Authenticating' requests")
97 101
       end
102
+
103
+      if options['code'].present? && options['code'].to_s !~ /\A\s*(\d+|\{.*)\s*\z/
104
+        errors.add(:base, "Must specify a code for request responses")
105
+      end
98 106
     end
99 107
 
100 108
     def payload_for(params)

+ 20 - 0
spec/models/agents/webhook_agent_spec.rb

@@ -59,6 +59,26 @@ describe Agents::WebhookAgent do
59 59
       expect(out).to eq(['Event Created', 201])
60 60
     end
61 61
 
62
+    it 'should respond with customized response code if configured with `code` option' do
63
+      agent.options['code'] = '200'
64
+      out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
65
+      expect(out).to eq(['Event Created', 200])
66
+    end
67
+
68
+    it 'should respond with `201` if the code option is empty, nil or missing' do
69
+      agent.options['code'] = ''
70
+      out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
71
+      expect(out).to eq(['Event Created', 201])
72
+      
73
+      agent.options['code'] = nil
74
+      out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
75
+      expect(out).to eq(['Event Created', 201])
76
+
77
+      agent.options.delete('code')
78
+      out = agent.receive_web_request({ 'secret' => 'foobar', 'some_key' => payload }, "post", "text/html")
79
+      expect(out).to eq(['Event Created', 201])
80
+    end
81
+
62 82
     describe "receiving events" do
63 83
 
64 84
       context "default settings" do